package coretest;

public class AluTest {
	
	private core.PsrClass psr = null;
	private core.AluClass alu = null;

	public AluTest(){
		psr = new core.PsrClass();
		alu = new core.AluClass(psr);
		this.ADDTests();
		this.SUBTests();
		this.ShiftTests();
		this.NotTests();
		this.BICTests();
	}
	
	public void ADDTests(){
		//Tests for ADD:
		//"Easy" tests:
		assert(alu.performOperation((short)0, (short)0, core.AluClass.Operations.ADD) == 0);
		assert(psr.getFlags() == 2);

		assert(alu.performOperation((short)1, (short) 2, core.AluClass.Operations.ADD) == 3);
		assert(psr.getFlags() == 0);
		
		//Tests for Negative
		assert(alu.performOperation((short)1, (short) -2, core.AluClass.Operations.ADD) == -1);
		assert(psr.getFlags() == 4);

		assert(alu.performOperation((short)1, (short) -32768, core.AluClass.Operations.ADD) == -32767);
		assert(psr.getFlags() == 4);

		assert(alu.performOperation((short)-1, (short) -32767, core.AluClass.Operations.ADD) == -32768);
		assert(psr.getFlags() == 5);

		//Tests for Carriage
		assert(alu.performOperation((short)32767, (short) 1, core.AluClass.Operations.ADD) == -32768);
		assert(psr.getFlags() == 12);

		assert(alu.performOperation((short)32767, (short) 32767, core.AluClass.Operations.ADD) == -2);
		assert(psr.getFlags() == 12);

		assert(alu.performOperation((short)-32766, (short) -2, core.AluClass.Operations.ADD) == -32768);
		assert(psr.getFlags() == 5);

		assert(alu.performOperation((short)-32767, (short) -2, core.AluClass.Operations.ADD) == 32767);
		assert(psr.getFlags() == 9);

		assert(alu.performOperation((short)-32768, (short) -32768, core.AluClass.Operations.ADD) == 0);
		assert(psr.getFlags() == 11);

		System.out.println("ALU - ADD OK");
	}
	public void SUBTests(){
		//Tests for SUB:
		//"Easy" tests:
		assert(alu.performOperation((short)0, (short)0, core.AluClass.Operations.SUB) == 0);
		assert(psr.getFlags() == 2);

		assert(alu.performOperation((short)1, (short) 2, core.AluClass.Operations.SUB) == -1);
		assert(psr.getFlags() == 4);

		//Tests for Negative
		assert(alu.performOperation((short)1, (short) -2, core.AluClass.Operations.SUB) == 3);
		assert(psr.getFlags() == 0);

		assert(alu.performOperation((short)1, (short) -32768, core.AluClass.Operations.SUB) == -32767);
		assert(psr.getFlags() == 12);

		assert(alu.performOperation((short)-1, (short) -32767, core.AluClass.Operations.SUB) == 32766);
		assert(psr.getFlags() == 1);

		//Tests for Carriage
		assert(alu.performOperation((short)32767, (short) 1, core.AluClass.Operations.SUB) == 32766);
		assert(psr.getFlags() == 1);

		assert(alu.performOperation((short)32767, (short) 32767, core.AluClass.Operations.SUB) == 0);
		assert(psr.getFlags() == 3);

		assert(alu.performOperation((short)-32766, (short) -2, core.AluClass.Operations.SUB) == -32764);
		assert(psr.getFlags() == 4);

		assert(alu.performOperation((short)-32767, (short) -2, core.AluClass.Operations.SUB) == -32765);
		assert(psr.getFlags() == 4);

		assert(alu.performOperation((short)-32768, (short) -32768, core.AluClass.Operations.SUB) == 0);
		assert(psr.getFlags() == 3);

		System.out.println("ALU - SUB OK");
	}	
	public void ShiftTests(){
		assert(alu.performOperation((short)0, (short)0, core.AluClass.Operations.SHL) == 0);
		assert(alu.performOperation((short)1, (short)0, core.AluClass.Operations.SHL) == 1);
		assert(alu.performOperation((short)0x0002, (short)6, core.AluClass.Operations.SHL) == 0x0080);
		assert(alu.performOperation((short)0x1234, (short)0x4, core.AluClass.Operations.SHL) == 0x2340);
		System.out.println("ALU - Shift OK");
	}
	
	public void NotTests(){
		assert(alu.performOperation((short)0, (short)32, core.AluClass.Operations.NOT)==(short)0xFFFDF);
		System.out.println("ALU - NOT OK");
	}
	
	public void BICTests(){
		assert(alu.performOperation((short)0xFFFF, (short)1, core.AluClass.Operations.BIC) == (short)0xfffe);
		assert(alu.performOperation((short)0x1111, (short)0x0101, core.AluClass.Operations.BIC) == (short)0x1010);
		System.out.println("ALU - BIC OK");
	}

	public static void main(String args[]){
		new AluTest();
	}
}
